package app.agent.tests

import app.Mode
import app.agent.tests.TestsLocator._
import app.comet.{RequestTests, Subscriber}
import app.server.TestsResponse
import app.ui.widget.{ExpandingToggle, ExpandingToggleParent}
import im.mange.jetboot.Html._
import im.mange.jetboot._
import im.mange.jetboot.js.JsCmdFactory._

//TODO: slide in the content
case class TestsAgent(subscriber: Subscriber) extends Renderable {
  private val contentAgent = TestsContentAgent(subscriber)
  private val content = span(R("Tests"))

  //TODO: I need an icon
  //TODO: show the test count as a badge <a href="#">Tests <span class="badge">42</span></a> - start with empty ...
  private val toggle = ExpandingToggle(testsToggle, content, "list-alt", new ExpandingToggleParent {
    override def onShow = showTests
    override def onHide = contentAgent.onHide //TODO: this looks common
  })

  override def render = div(id = testsAgent, toggle, contentAgent).classes(if (Mode.isDemo) "hidden" else "").render

  def onShowResponse(response: TestsResponse) = toggle.stopSpinner & contentAgent.onShowResponse(response)

  //TODO: should this go upto parent.xyz instead?
  private[agent] def showTests = {
    subscriber ! RequestTests
    nothing
  }
}